package com.wuji.salary.common;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.taobao.api.ApiException;
import org.springframework.context.annotation.ComponentScan;

import static com.wuji.salary.common.GetToken.token;


/**
 * 根据  userId 去获取用户名字
 */
@ComponentScan
public class GetUserInfo {
    public static void main(String[] args) {
        // 592847366737895903
        OapiV2UserGetResponse.UserGetResponse userInfo = getUserInfo("331004400521732855");
        getUserInfo("331004400521732855");
//        System.out.println(userInfo);
//        String deptName = getDeptName(935832979L);
//        System.out.println(deptName);
    }
    /**
     * 根据 userId 去查询姓名
     */
    public static OapiV2UserGetResponse.UserGetResponse getUserInfo(String userId){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(userId);
            OapiV2UserGetResponse rsp = client.execute(req, token);
            System.out.println(rsp.getBody());
            OapiV2UserGetResponse.UserGetResponse result = rsp.getResult();
            return result;
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *   根据部门 id 去获取部门名字
     */
    public static String getDeptName(Long deptIdList){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
            OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
            req.setDeptId(deptIdList);
            OapiV2DepartmentGetResponse rsp = client.execute(req, token);
            System.out.println(rsp.getBody());
            return rsp.getResult().getName();
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }
}
